package com.tanhua.dubbo.api.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Pan Rundong
 * @Date 2023-02-25 21:08
 * @Desc 用户详细信息Api实现类
 */
@DubboService(version = "1.0.0")
public class UserInfoApiImpl implements UserInfoApi {
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 保存
     * @Author: Pan Rundong
     * @param userInfo
     * @return void
     */
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /**
     * 根据id查询
     * @Author: Pan Rundong
     * @param id
     * @return com.tanhua.model.domain.UserInfo
     */
    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    /**
     * 更新
     * @Author: Pan Rundong
     * @param userInfo
     * @return void
     */
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 查询推荐用户信息
     *
     * @param ids
     * @param age
     * @param gender
     * @return java.util.Map
     * @Author Pan Rundong
     */
    @Override
    public Map<Long, UserInfo> recommendation(List<Long> ids, Integer age, String gender) {
        //1.新建map集合
        Map<Long, UserInfo> map = new HashMap<>();
        //2.构建条件判断，查询list
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId, ids);
        queryWrapper.lt(age != null, UserInfo::getAge, age);
        queryWrapper.eq(gender != null, UserInfo::getGender, gender);
        List<UserInfo> userInfos = userInfoMapper.selectList(queryWrapper);

        //3.存信息进map
        userInfos.forEach((item)->{
            map.put(item.getId(), item);
        });

        //4.返回map
        return map;
    }

    /**
     * 分页查询推荐用户信息
     *
     * @param longs
     * @param age
     * @param gender
     * @return com.tanhua.model.vo.PageResult
     * @Author Pan Rundong
     */
    @Override
    public Page<UserInfo> recommendationPage(Integer page, Integer pageSize,
                                             List<Long> longs, Integer age, String gender) {
        //1.构建Page对象
        Page<UserInfo> pageInfo = new Page<>(page, pageSize);

        //1.构建条件
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(longs != null, UserInfo::getId, longs);
        queryWrapper.lt(age != null, UserInfo::getAge, age);
        queryWrapper.eq(gender != null, UserInfo::getGender, gender);

        //2.查询数据库
        userInfoMapper.selectPage(pageInfo, queryWrapper);

        //3.返回
        return pageInfo;
    }

    /**
     * 根据userIds查询
     *
     * @param userIds
     * @param info
     * @return java.util.Map<java.lang.Long, com.tanhua.model.domain.UserInfo>
     * @Author Pan Rundong
     */
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo info) {
        QueryWrapper qw = new QueryWrapper();
        //1、用户id列表
        qw.in("id",userIds);
        //2、添加筛选条件
        if(info != null) {
            if(info.getAge() != 0) {
                qw.lt("age",info.getAge());
            }
            if(!StringUtils.isEmpty(info.getGender())) {
                qw.eq("gender",info.getGender());
            }
            if(!StringUtils.isEmpty(info.getNickname())) {
                qw.like("nickname",info.getNickname());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    /**
     * 用户分页查找
     *
     * @param page
     * @param pagesize
     * @return com.tanhua.model.vo.PageResult
     * @Author Pan Rundong
     */
    @Override
    public IPage<UserInfo> getPage(Integer page, Integer pagesize) {
        //1.构建Page对象
        Page<UserInfo> pageInfo = new Page<>(page, pagesize);

        //1.构建条件
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();

        //2.查询数据库
        userInfoMapper.selectPage(pageInfo, queryWrapper);

        //3.返回
        return pageInfo;
    }
}
